package com.disruptor.sample._03config;

import com.disruptor.sample._01base.consumer.LongEventHandler;
import com.disruptor.sample._01base.factory.LongEventFactory;
import com.disruptor.sample._02producer.procucer.LongEventProducer;
import com.disruptor.sample.common.dto.LongEvent;
import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;
import com.lmax.disruptor.util.DaemonThreadFactory;
import java.nio.ByteBuffer;

public class LongEventMainByConfig {

  public static void main(String[] args) throws Exception {
    LongEventFactory factory = new LongEventFactory();

    int bufferSize = 1024;
    Disruptor<LongEvent> disruptor =
        new Disruptor<>(
            factory,
            bufferSize,
            DaemonThreadFactory.INSTANCE,
            // 1 单个生产者与多个生产者
            ProducerType.MULTI,
            // 2 等待策略，其他策略如 SleepingWaitStrategy、YieldingWaitStrategy、BusySpinWaitStrategy
            // WaitStrategyDisruptor 默认使用的是 BlockingWaitStrategy.在内部 BlockingWaitStrategy 使用典型的锁和条件变量来处理线程唤醒。
            // 这 BlockingWaitStrategy 是可用等待策略中最慢的，但在 CPU 使用率方面是最保守的，并且将在最广泛的部署选项中提供最一致的行为。
            new BlockingWaitStrategy()
        );

    disruptor.handleEventsWith(new LongEventHandler());
    disruptor.start();

    RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();
    LongEventProducer producer = new LongEventProducer(ringBuffer);
    ByteBuffer bb = ByteBuffer.allocate(8);
    for (long l = 0; true; l++) {
      bb.putLong(0, l);
      producer.onData(bb);
      Thread.sleep(1000);
    }
  }

}
